package com.lixixi.tingshu.common.handler;

import com.lixixi.tingshu.common.result.Result;
import com.lixixi.tingshu.common.result.ResultCodeEnum;
import com.lixixi.tingshu.exception.BaseException;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(BaseException.class)
	Result removeAlbumExceptionHandler(BaseException exception) {
		ResultCodeEnum resultCodeEnum = exception.getResultCodeEnum();
		if(resultCodeEnum == null)
			return Result.build(null, exception.getCode(), exception.getMessage());
		return Result.build(null, resultCodeEnum.getCode(), resultCodeEnum.getMessage());
	}

	@ExceptionHandler(TencentCloudSDKException.class)
	Result tencentCloudSDKException(TencentCloudSDKException exception) {
		log.debug(exception.getMessage());
		return Result.build(null, ResultCodeEnum.VOD_FILE_MESSAGE_GET_ERROR.getCode(), ResultCodeEnum.VOD_FILE_MESSAGE_GET_ERROR.getMessage());
	}

	@ExceptionHandler(WxErrorException.class)
	Result wxErrorException(WxErrorException exception){
		return Result.build(null, exception.getError().getErrorCode(), exception.getError().getErrorMsg());
	}
}
